﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MySkiPatrolSite.Controllers
{
    public class ArticleController : Controller
    {
        ArticleSvc.ArticleClient svc = new ArticleSvc.ArticleClient();
        //
        // GET: /Article/

        public ActionResult New()
        {
            ArticleSvc.Article model = new ArticleSvc.Article();
            model.DataTransaction = new ArticleSvc.DataTransaction();
            model.DataTransaction.PatrolPrefix = ((AdminSvc.Patrol)Session["PATROL"]).dbPrefix;

            //default dates
            model.EffectiveDate = DateTime.Today;
            model.ExpiryDate = DateTime.Today.AddMonths(1);

            return View(model);
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult New(ArticleSvc.Article model, FormCollection form)
        {
            
            model.DataTransaction = new ArticleSvc.DataTransaction();
            model.DataTransaction.PatrolPrefix = ((AdminSvc.Patrol)Session["PATROL"]).dbPrefix;

            return View(model);
        }

        public ActionResult Edit(string id)
        {
            ArticleSvc.Article model = new ArticleSvc.Article();
            model.ID = id;
            model.DataTransaction = new ArticleSvc.DataTransaction();
            model.DataTransaction.PatrolPrefix = ((AdminSvc.Patrol)Session["PATROL"]).dbPrefix;

            model = svc.GetArticle(model);

            return View(model);
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(string id, ArticleSvc.Article model, FormCollection form)
        {
            if(model.DataTransaction == null) model.DataTransaction = new ArticleSvc.DataTransaction();
            if (model.ID == null) model.ID = form.GetValue("ID").AttemptedValue;
            model.DataTransaction.PatrolPrefix = ((AdminSvc.Patrol)Session["PATROL"]).dbPrefix;

            //need to set the dates...
            model.ExpiryDate = DateTime.Parse(form.GetValue("ExpiryDate").AttemptedValue);
            model.EffectiveDate = DateTime.Parse(form.GetValue("EffectiveDate").AttemptedValue);

            switch (form.GetValue("action").AttemptedValue.ToUpper())
            {
                case "DELETE":
                    model.DataTransaction.Action = ArticleSvc.DataAction.Delete;
                    break;
                case "SAVE":
                    model.DataTransaction.Action = ArticleSvc.DataAction.Update;
                    break;
                case "CANCEL":
                    model.DataTransaction.Action = ArticleSvc.DataAction.View;
                    break;
            }

            

            model = svc.PutArticle(model);

            return View(model);
        }

        public ActionResult Delete(string id)
        {
            ArticleSvc.Article model = new ArticleSvc.Article();
            model.ID = id;
            model.DataTransaction = new ArticleSvc.DataTransaction();
            model.DataTransaction.PatrolPrefix = ((AdminSvc.Patrol)Session["PATROL"]).dbPrefix;

            model.DataTransaction.Action = ArticleSvc.DataAction.Delete;
            model = svc.PutArticle(model);

            //check the status of the delete...


         
            return RedirectToAction("Index", "Home");
        }

    }


}
